﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebApi2b.Data.Entity;
using WebApi2b.Domain.Enum;
using WebApi2b.Domain.Interface;
using WebApi2b.Interface.Log;

namespace WebApi2b.Implement.Log
{
    public class Log : ILog
    {

        private readonly IRespository<Logs> _IRespository;

        public  Log(IRespository<Logs> IRespository)
        {
            _IRespository = IRespository;
        }
        public void Clear()
        {
            var list = _IRespository.Table.ToList();
            _IRespository.Delete(list);
        }

        public void Delete(Logs log)
        {
            _IRespository.Delete(log);
        }

        public Logs GetLogById(int id)
        {
            return _IRespository.GetById(id);
        }

        public void Insert(Logs log)
        {
            _IRespository.Insert(log);
        }

        public IEnumerable<Logs> MultiQuery(DateTime beginTime, DateTime endTime, LogsLevel logLevel=LogsLevel.错误, int UserId = 0)
        {
            var bTime = beginTime == null ? DateTime.Now : beginTime;
            var eTime = endTime == null ? DateTime.Now : endTime;

            var list = _IRespository.Table.Where(x => x.CreateTime >= beginTime && x.CreateTime <= endTime && x.LogsLevel == (int)logLevel);

            if (UserId != 0) 
            {
                list = list.Where(x => x.UserId == UserId);
            }

            return list;
        }
    }
}
